package hos.file.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import hos.file.R;

/**
 * <p>Title: FileDialog </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-04-15 10:51
 */
public class FileDialog extends Dialog {

    public static final int TYPE_IMAGE = 0;
    public static final int TYPE_VIDEO = 1;
    public static final int TYPE_IMAGE_AUDIO = 2;
    public static final int TYPE_IMAGE_VIDEO = 3;
    public static final int TYPE_IMAGE_VIDEO_AUDIO = 4;
    public static final int TYPE_IMAGE_VIDEO_AUDIO_FILE = 5;
    public static final int TYPE_IMAGE_FILE = 6;
    public static final int TYPE_VIDEO_FILE = 7;
    public static final int TYPE_AUDIO_FILE = 8;
    public static final int TYPE_IMAGE_VIDEO_FILE = 9;
    private TextView mCreateImage;
    private View mCreateImageLine;
    private TextView mCreateVideo;
    private View mCreateVideoLine;
    private TextView mCreateAudio;
    private View mCreateAudioLine;
    private TextView mChooseImage;
    private View mChooseImageLine;
    private TextView mChooseVideo;
    private View mChooseVideoLine;
    private TextView mChooseFile;
    private View mChooseFileLine;
    private TextView mCancel;

    private final int mType;

    private final OnFileClickListener mOnFileClickListener;

    public FileDialog(Context context, int type, OnFileClickListener onFileClickListener) {
        super(context, R.style.DefaultDialog);
        this.mType = type;
        mOnFileClickListener = onFileClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_image_video_audio_file);
        initView();
        setupType();
//        buildPermissionsLayout();
        setupWindow();
    }

    private void setupWindow() {
        Window window = getWindow();
        if (window == null) {
            return;
        }
        int width = getContext().getResources().getDisplayMetrics().widthPixels;
//        int height = getContext().getResources().getDisplayMetrics().heightPixels;
        WindowManager.LayoutParams param = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
//        if (width < height) {
//            // now we are in portrait
//            param.width = (int) (width * 0.86);
//        } else {
//            // now we are in landscape
//            param.width = (int) (width * 0.6);
//        }
        param.width = width;
        window.setAttributes(param);
    }

    private void setupType() {
        if (mType == TYPE_IMAGE) {
            setVisibility(View.VISIBLE, mCreateImage, mCreateImageLine,
                    mChooseImage, mChooseImageLine);
            setVisibility(View.GONE, mCreateVideo, mCreateVideoLine,
                    mCreateAudio, mCreateAudioLine,
                    mChooseVideo, mChooseVideoLine,
                    mChooseFile, mChooseFileLine);
        } else if (mType == TYPE_IMAGE_AUDIO) {
            setVisibility(View.VISIBLE, mCreateImage, mCreateImageLine,
                    mCreateAudio, mCreateAudioLine,
                    mChooseImage, mChooseImageLine);
            setVisibility(View.GONE, mCreateVideo, mCreateVideoLine,
                    mChooseVideo, mChooseVideoLine,
                    mChooseFile, mChooseFileLine);
        } else if (mType == TYPE_IMAGE_VIDEO) {
            setVisibility(View.VISIBLE, mCreateImage, mCreateImageLine,
                    mCreateVideo, mCreateVideoLine,
                    mChooseVideo, mChooseVideoLine,
                    mChooseImage, mChooseImageLine);
            setVisibility(View.GONE,
                    mCreateAudio, mCreateAudioLine,
                    mChooseFile, mChooseFileLine);
        } else if (mType == TYPE_IMAGE_VIDEO_AUDIO) {
            setVisibility(View.VISIBLE, mCreateImage, mCreateImageLine,
                    mCreateAudio, mCreateAudioLine,
                    mCreateVideo, mCreateVideoLine,
                    mChooseVideo, mChooseVideoLine,
                    mChooseImage, mChooseImageLine);
            setVisibility(View.GONE,
                    mChooseFile, mChooseFileLine);
        } else if (mType == TYPE_IMAGE_VIDEO_AUDIO_FILE) {
            setVisibility(View.VISIBLE, mCreateImage, mCreateImageLine,
                    mCreateAudio, mCreateAudioLine,
                    mCreateVideo, mCreateVideoLine,
                    mChooseFile, mChooseFileLine
            );
            setVisibility(View.GONE,
                    mChooseVideo, mChooseVideoLine,
                    mChooseImage, mChooseImageLine
            );
        } else if (mType == TYPE_IMAGE_VIDEO_FILE) {
            setVisibility(View.VISIBLE, mCreateImage, mCreateImageLine,
                    mCreateVideo, mCreateVideoLine,
                    mChooseFile, mChooseFileLine
            );
            setVisibility(View.GONE,
                    mCreateAudio, mCreateAudioLine,
                    mChooseVideo, mChooseVideoLine,
                    mChooseImage, mChooseImageLine
            );
        } else if (mType == TYPE_VIDEO) {
            setVisibility(View.VISIBLE,
                    mCreateVideo, mCreateVideoLine,
                    mChooseVideo, mChooseVideoLine
            );
            setVisibility(View.GONE, mCreateImage, mCreateImageLine,
                    mCreateAudio, mCreateAudioLine,
                    mChooseImage, mChooseImageLine,
                    mChooseFile, mChooseFileLine
            );
        } else if (mType == TYPE_VIDEO_FILE) {
            setVisibility(View.VISIBLE,
                    mCreateVideo, mCreateVideoLine,
                    mChooseFile, mChooseFileLine
            );
            setVisibility(View.GONE, mCreateImage, mCreateImageLine,
                    mCreateAudio, mCreateAudioLine,
                    mChooseImage, mChooseImageLine,
                    mChooseVideo, mChooseVideoLine
            );
        } else if (mType == TYPE_IMAGE_FILE) {
            setVisibility(View.VISIBLE, mCreateImage, mCreateImageLine,
                    mChooseFile, mChooseFileLine
            );
            setVisibility(View.GONE, mCreateVideo, mCreateVideoLine,
                    mCreateAudio, mCreateAudioLine,
                    mChooseImage, mChooseImageLine,
                    mChooseVideo, mChooseVideoLine
            );
        } else if (mType == TYPE_AUDIO_FILE) {
            setVisibility(View.VISIBLE, mCreateAudio, mCreateAudioLine,
                    mChooseFile, mChooseFileLine
            );
            setVisibility(View.GONE, mCreateVideo, mCreateVideoLine,
                    mCreateImage, mCreateImageLine,
                    mChooseImage, mChooseImageLine,
                    mChooseVideo, mChooseVideoLine
            );
        }
    }

    private void initView() {
        mCreateImage = (TextView) findViewById(R.id.createImage);
        mCreateImageLine = (View) findViewById(R.id.createImageLine);
        mCreateVideo = (TextView) findViewById(R.id.createVideo);
        mCreateVideoLine = (View) findViewById(R.id.createVideoLine);
        mCreateAudio = (TextView) findViewById(R.id.createAudio);
        mCreateAudioLine = (View) findViewById(R.id.createAudioLine);
        mChooseImage = (TextView) findViewById(R.id.chooseImage);
        mChooseImageLine = (View) findViewById(R.id.chooseImageLine);
        mChooseVideo = (TextView) findViewById(R.id.chooseVideo);
        mChooseVideoLine = (View) findViewById(R.id.chooseVideoLine);
        mChooseFile = (TextView) findViewById(R.id.chooseFile);
        mChooseFileLine = (View) findViewById(R.id.chooseFileLine);
        mCancel = (TextView) findViewById(R.id.cancel);
        mCreateImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnFileClickListener.onCreateImage(FileDialog.this, getContext());
                dismiss();
            }
        });
        mCreateVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnFileClickListener.onCreateVideo(FileDialog.this, getContext());
                dismiss();
            }
        });
        mCreateAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnFileClickListener.onCreateAudio(FileDialog.this, getContext());
                dismiss();
            }
        });
        mChooseImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnFileClickListener.onChooseImage(FileDialog.this, getContext());
                dismiss();
            }
        });
        mChooseVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnFileClickListener.onChooseVideo(FileDialog.this, getContext());
                dismiss();
            }
        });
        mChooseFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnFileClickListener.onChooseFile(FileDialog.this, getContext());
                dismiss();
            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void setVisibility(int visibility, View... views) {
        if (views == null) {
            return;
        }
        for (View view : views) {
            view.setVisibility(visibility);
        }
    }
}
